package viv.karaoke.book;

import java.util.List;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.view.View.OnClickListener;

public class FavouriteFragment extends Fragment
{
	private ListView lvProfile;
	private ListView lvSong;
	private Button btnAddProfile;
	private Button btnRenameProfile;
	private Button btnRemoveProfile;
	
	private MainActivity activity;
	
	private ProfileAdapter profileAdapter;
	private SongAdapter songAdapter;
	
	private user selectedProfile;
	
	@Override
	public View onCreateView( LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState )
	{	
		View view = inflater.inflate(R.layout.favourite, container, false);
		
		lvProfile =  (ListView) view.findViewById(R.id.lvProfile);
		lvSong =  (ListView) view.findViewById(R.id.lvSong);
		
		btnAddProfile = (Button)view.findViewById(R.id.btnAddFavorite);
		btnRenameProfile = (Button)view.findViewById(R.id.btnRenameFavorite);
		btnRemoveProfile = (Button)view.findViewById(R.id.btnRemoveFavorite);
		
		return view;
	}
	
	@Override
	  public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);
	    	    
	    activity = (MainActivity)getActivity();
	    
	    if (activity != null) {
	    	
	    	userDataSource userDs = new userDataSource(activity);
	    	userDs.open();
	    	
	    	List<user> profileList = userDs.getAllUsers();
	    	
	    	profileAdapter = new ProfileAdapter(activity, viv.karaoke.book.R.layout.profile_listview_item_row, profileList);
	    	
	    	lvProfile.setAdapter(profileAdapter);
	    	
	    	lvProfile.setOnItemClickListener(new OnItemClickListener() {
	            
	            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//	            	String item = ((TextView)view).getText().toString();
	                selectedProfile = (user)lvProfile.getItemAtPosition(position);
	            	
	            	// rebind data for lvSong
	                ufsDataSource profileSongDataSource = new ufsDataSource(activity);
	                profileSongDataSource.open();
	                
	                songAdapter = new SongAdapter(activity, profileSongDataSource.getAllSongByUserId(selectedProfile.getUserId()));
	                
	                MenuItem profileNameMenu = activity.getMainMenu().findItem(R.id.menu_profile_name);
	                
	                profileNameMenu.setTitle("Profile name: " + selectedProfile.getUserName());
	                
	                Toast.makeText(activity.getApplicationContext(), selectedProfile.getUserName() + " has been selected!" , Toast.LENGTH_LONG).show();
	                
	            }
	        });
	    	
//	    	ProfileAdapter songAdapter = new ProfileAdapter(activity, viv.karaoke.book.R.layout.profile_listview_item_row, songNames);
	    	SongDataSource songDataSource = new SongDataSource(activity, 1);
	    	songDataSource.open();
	    	
	    	List<song> songList = songDataSource.getAllSongs("", 0);
	    	
	    	songAdapter = new SongAdapter(activity, songList);
	    	
	    	lvSong.setAdapter(songAdapter);
	    	
//	    	handle event for button
	    	btnAddProfile.setOnClickListener(new OnClickListener()
	        {
	    		@Override
	    	    public void onClick(View v)
	    	    {
	    			
	    			Toast.makeText(activity.getApplicationContext(), "Add Profile !" , Toast.LENGTH_LONG).show();
	    	    }
	    	});
	    	
	    	btnRenameProfile.setOnClickListener(new OnClickListener()
	        {
	    		@Override
	    	    public void onClick(View v)
	    	    {
	    			Toast.makeText(activity.getApplicationContext(), "Rename Profile !" , Toast.LENGTH_LONG).show();
	    	    }
	    	});
	    	
	    	btnRemoveProfile.setOnClickListener(new OnClickListener()
	        {
	    		@Override
	    	    public void onClick(View v)
	    	    {	
	    			user selectedProfile = (user)lvProfile.getSelectedItem();
	    			
	    			// remove profile from database
	    			
	    			
	    			profileAdapter.RemoveProfile(lvProfile.getSelectedItemPosition());
	    			profileAdapter.notifyDataSetChanged();
	    			
	    			Toast.makeText(activity.getApplicationContext(), "Profile " + selectedProfile.getUserName() + " has been removed!" , Toast.LENGTH_SHORT).show();
	    	    }
	    	});
	    }
		//ListView listView =  (ListView) getActivity().findViewById(R.id.lvProfile);
		//listView.setAdapter(new ProfileAdapter(this.getActivity(), values));
		
	    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
	    //    android.R.layout.simple_list_item_1, values);
	    
	    //setListAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),
        //        android.R.layout.simple_list_item_1, values));
	    
	    //setListAdapter(adapter);
	  }
}
